package com.github.llyb120.nami.ctrl;

import com.beeasy.hzbpm.exception.BpmException;
import com.github.llyb120.nami.core.RestException;
import com.github.llyb120.json.Obj;
import com.github.llyb120.nami.server.Response;

import java.util.Map;

import static com.github.llyb120.json.Json.o;

public abstract class Ctrl {
    protected Obj $get = o();
    protected Obj $post = o();
    protected Obj $request = o();

    public void onBind(Response response){
        if (response.request.query != null) {
            $get.putAll(response.request.query);
        }
        if (response.request.body instanceof Map) {
            $post.putAll((Map<? extends String, ?>) response.request.body);
        }
        if (response.request.params != null) {
            $request.putAll(response.request.params);
        }
    }

    protected void error(String msg, Object ...args){
        throw new BpmException(String.format(msg, args));
    }
}
